/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.common.util;

import java.io.Serializable;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>Contains list of folders and provides some operations for it.
 * This class is backed by instance of <code>java.util.List</code></p>
 * <p><a href="FolderList.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.7 $ $Date: 2006/08/03 10:07:33 $
 */
public class FolderList implements Serializable {

	protected LinkedList folders = new LinkedList();
	/**
	 * Delimiter between folder names in string
	 */
	protected char delimiter = '/';

	/**
	 * Creates new instance of FolderList.
	 */
	public FolderList() {
		this(new LinkedList());
	}

	/**
	 * Creates new instance of FolderList from a collection of folders.
	 *
	 * @param folders Collection of folders
	 */
	public FolderList(Collection folders) {
		this(folders, '/');
	}

	/**
	 * Creates new instance of FolderList from a collection of folders with
     * a given delimiter.
	 *
	 * @param folders   List of folders
	 * @param delimiter Delimiter between folder names in path
	 */
	public FolderList(Collection folders, char delimiter) {
		this.folders = new LinkedList(folders);
		this.delimiter = delimiter;
	}

    /**
     * Sets delimiter between folder names
     *
     * @param delimiter the string that will serve as a delimiter
     */
	public void setDelimiter(char delimiter) {
		this.delimiter = delimiter;
	}

    /**
     * Adds {@link Folder} to list
     *
     * @param folder the folder to add to list
     * @return <code>true</code>
     * @see com.blandware.atleap.common.util.Folder
     */
	public boolean addFolder(Folder folder) {
		return folders.add(folder);
	}

    /**
     * Clears folder list
     */
	public void clear() {
		folders.clear();
	}

    /**
     * Gets a <code>List</code> of folders
     *
     * @return list of folders
     */
	public List getFolders() {
		return folders;
	}

    /**
     * Gets a number of folders in list
     *
     * @return number of folders in list
     */
	public int getNumberOfFolders() {
		return folders.size();
	}

    /**
     * Gets the last folder in list, if there are some folders, or <code>null</code>
     * otherwise
     *
     * @return last folder in list, or <code>null</code>
     */
	public Folder getLastFolder() {
		if ( !folders.isEmpty() ) {
			return (Folder) folders.getLast();
		} else {
			return null;
		}
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(delimiter);
		for ( Iterator i = folders.iterator(); i.hasNext(); ) {
			Folder folder = (Folder) i.next();
			sb.append(folder.getName()).append(delimiter);
		}
		return sb.toString();
	}


}
